import { ref } from 'vue'
import { defineStore } from 'pinia'
import { computedAsync } from '@vueuse/core'
import api from '@/api/chapter'
import type { ChapterForm } from '@/config/forms'
import type { HashSet, LessonItem, ChapterItem } from '@/config/types'


export const useChapterStore = defineStore('chapter', () => {
  const loading = ref<boolean>(false)
  const chapters = ref<ChapterItem[]>([])
  const currentLessonId = ref<number>(0)
  //课程缓存
  const lessonCaches = ref<HashSet<LessonItem>>({})

  const updating = ref<boolean>(false)
  const chapter = ref<ChapterItem | null>(null)

  const lesson = computedAsync(async () => {
    if (currentLessonId.value === 0) {
      return null
    }
    if (lessonCaches.value[currentLessonId.value]) {
      return lessonCaches.value[currentLessonId.value]
    }
    await loadLesson(currentLessonId.value)
    return lessonCaches.value[currentLessonId.value]
  }, null)

  async function loadChapters(lessonId: number) {
    loadLesson(lessonId)
    loading.value = true
    try {
      const res = await api.getList(lessonId)
      chapters.value = res.data
      loading.value = false
    } catch (e) {
      loading.value = false
    }
  }

  async function loadLesson(lessonId: number) {
    currentLessonId.value = lessonId
    if (!lessonCaches.value[lessonId]) {
      const res = await api.getLesson(currentLessonId.value)
      lessonCaches.value[currentLessonId.value] = res.data
    }
  }

  async function loadChapter(id: number) {
    updating.value = true
    try {
      const res = await api.getItem(id)
      chapter.value = res.data
      updating.value = false
    } catch (e) {
      updating.value = false
    }
  }

  async function saveChapter() {
    updating.value = true
    let result = false
    try {
      if (chapter.value) {
        const form: ChapterForm = {
          lessonId: chapter.value.lessonId,
          title: chapter.value.title,
          video: chapter.value.video,
          duration: chapter.value.duration,
          sort: chapter.value.sort
        }
        let res
        if (chapter.value.id > 0) {
          res = await api.update(chapter.value.id, form)
        } else {
          res = await api.create(form)
        }
        result = res.data
      }
      updating.value = false
    } catch (e) {
      updating.value = false
    }
    return result
  }

  async function removeChapter(id: number) {
    updating.value = true
    try {
      const res = await api.delete(id)
      updating.value = false
      return res.data
    } catch (e) {
      updating.value = false
    }
    return false
  }

  function newChapter() {
    chapter.value = {
      id: 0,
      lessonId: currentLessonId.value,
      lesson: lesson.value?.title || '',
      title: '',
      video: '',
      duration: 0,
      sort: 0,
      onself: false,
      updatedAt: 0
    }
  }

  async function updateOnself(val: boolean) {
    const ids = chapters.value.filter((item) => item.onself === val).map((item) => item.id)
    await api.onself(ids, val)
  }

  return {
    loading,
    lesson,
    chapters,
    loadLesson,
    loadChapters,
    updateOnself,

    updating,
    chapter,
    loadChapter,
    newChapter,
    saveChapter,
    removeChapter
  }
})
